﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using XYAuto.MonitorData.Chitunion.Bll.Provider.Dto;
using XYAuto.MonitorData.Chitunion.Bll.Provider.Dto.Request;
using XYAuto.MonitorData.Chitunion.Infrastruct.Verification;

namespace XYAuto.MonitorData.Chitunion.Bll.Provider
{
    public class MonitorTaskInfoProvider : VerifyOperateBase
    {
        private readonly ConfigEntity _configEntity;
        private readonly ReqCreateMonitorTaskDto _reqCreateMonitorTaskDto;

        public MonitorTaskInfoProvider(ConfigEntity configEntity, ReqCreateMonitorTaskDto reqCreateMonitorTaskDto)
        {
            _configEntity = configEntity;
            _reqCreateMonitorTaskDto = reqCreateMonitorTaskDto;
        }

        public ReturnValue Create()
        {
            var retValue = VerifyCreate();
            if (retValue.HasError)
            {
                return retValue;
            }

            var excuteId = Dal.MonitorTaskInfo.Instance.Insert(GetEntity(_reqCreateMonitorTaskDto));
            if (excuteId <= 0)
            {
                return CreateFailMessage(retValue, "2001", "添加任务失败");
            }
            return retValue;

        }

        private Entities.MonitorTaskInfo GetEntity(ReqCreateMonitorTaskDto dto)
        {
            var entity = new Entities.MonitorTaskInfo()
            {
                MonitorName = dto.MonitorName,
                AchieveCount = dto.AchieveCount,
                CreateTime = DateTime.Now,
                UpdateTime = DateTime.Now,
                CreateUserId = _configEntity.CreateUserId,
                DataConnect = "",
                DataServceId = dto.DataServceId,
                IsActive = true,
                MonitorSql = dto.MonitorSql,
                NoticeKey = dto.NoticeKey,
                NoticeToValue = dto.NoticeToValue,
                NoticeDesc = dto.NoticeDesc
            };

            return entity;
        }

        private ReturnValue VerifyCreate()
        {
            var retValue = VerifyOfNecessaryParameters(_reqCreateMonitorTaskDto);
            if (retValue.HasError)
                return retValue;
            return retValue;
        }
    }
}
